package list.model;

import list.control.MysqlLink;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by ZhipengLi.
 */
public class User extends MysqlLink {
    private static final String TABLE = "user";

    private int user_id;

    private String id;

    private String email;

    private String birthday;

    private String head;

    private String phone;

    private Tag[] tags;

    private Contact[] contacts;

    private int numOfContacts;

    public User(int user_id) {
        this.user_id = user_id;
    }

    public User(String id, String phone, int user_id, String birthday, String head) {
        this(user_id);
        this.id = id;
        this.phone = phone;
        this.birthday = birthday;
        this.head = head;
    }

    public static boolean login(String idOrPhone, String password) {
        ResultSet resultSet = select(TABLE, ALL, equalsOr(toArray("id", "phone"), toArray(idOrPhone, idOrPhone)));
        if (null != resultSet) {
            try {
                if (resultSet.next()) {
                    String true_password = resultSet.getString("password");
                    resultSet.close();
                    /*if (true_password.equals(password)) {
                        int user_id = resultSet.getInt("user_id");
                        String id = resultSet.getString("id");
                        String phone = resultSet.getString("phone");
                        String birthday = resultSet.getString("birthday");
                        String head = resultSet.getString("head");
                        User user = new User(id, phone, user_id, birthday, head);
                        user.tags = Tag.select(user_id);
                    }*/
                    return true_password.equals(password);
                }
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public static boolean register(String id, String email, String password) {
        return !isExisted(id, email) && insert(TABLE, toArray("id", "email", "password"), toArray(id, email, password));
    }

    private static boolean isExisted(String id, String phone) {
        return exist(TABLE, "id", equalsOr(toArray("id", "phone"), toArray(id, phone)));
    }

    public void init() {
        ResultSet resultSet = select(TABLE, ALL, equal("user_id", user_id));
        if (null != resultSet) {
            try {
                if (resultSet.next()) {
                    this.id = resultSet.getString("id");
                    this.phone = resultSet.getString("phone");
                    this.birthday = resultSet.getString("birthday");
                    this.head = resultSet.getString("head");
                }
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public boolean updatePassword(String old_password, String new_password) {
        return update(TABLE, "password", new_password, equalsAnd(toArray("user_id", "password"), toArray(user_id, old_password)));
    }

    private boolean updateField(String key, Object value) {
        return update(TABLE, key, value, equal("user_id", user_id));
    }

    public boolean updateEmail(String new_email) {
        return updateField("email", new_email);
    }

    public boolean updateName(String new_name) {
        return updateField("name", new_name);
    }

    public boolean updateBirthday(String new_birthday) {
        return updateField("birthday", new_birthday);
    }

    public boolean updateHead(String new_head) {
        return updateField("head", new_head);
    }

    public boolean updatePhone(String new_phone) {
        return updateField("phone", new_phone);
    }

    public boolean insertContact(int tag_id) {
        return Contact.insert(user_id, tag_id);
    }

    public boolean deleteContact(int tag_id, int contact_id) {
        return Contact.delete(user_id, tag_id, contact_id);
    }

    public boolean updateContact(int contact_id, String property, Object value) {
        return Contact.update(user_id, contact_id, property, value);
    }

    public Contact[] getContacts(int tag_id) {
        if (null == this.contacts) {
            this.contacts = Contact.select(user_id, tag_id);
        }
        return this.contacts;
    }

    public Contact[] getContacts() {
        return Contact.select(user_id);
    }

    private void setNumOfContacts(){
        int count = 0;
        for (Tag tag : this.getTags()) {
            count += tag.getCount();
        }
        this.numOfContacts = count;
    }

    public int getNumOfContacts() {
        return this.numOfContacts;
    }

    public boolean insertTag() {
        return Tag.insert(user_id);
    }

    public boolean deleteTag(int tag_id) {
        return Tag.delete(user_id, tag_id) && Contact.delete(user_id, tag_id);
    }

    public boolean updateTag(int tag_id, String tag_name) {
        return Tag.update(user_id, tag_id, tag_name);
    }

    private void selectTag() {
        this.tags = Tag.select(user_id);
    }

    public int getUser_id() {
        return user_id;
    }

    public String getId() {
        return id;
    }

    public String getEmail() {
        return email;
    }

    public String getBirthday() {
        return birthday;
    }

    public String getHead() {
        return head;
    }

    public String getPhone() {
        return phone;
    }

    public Tag[] getTags() {
        if (null == this.tags) {
            this.selectTag();
        }
        return tags;
    }
}
